/* *
 * File: AchievementManager.java
 * @author: Adam Phung
 * @author: Abdelghani Bhalla
 * Email: adam.phung@gmail.com
 * 
 * Date Created: Oct 30, 2012
 * Date Modified: Oct 31, 2012 
 */

package edu.bu.cs673.superkids123.parents.DataObjectAccess;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import edu.bu.cs673.superkids123.HibernateUtil;
import edu.bu.cs673.superkids123.common.model.Achievement;
import edu.bu.cs673.superkids123.common.model.Child;

public class AchievementManager {
	
	/**
	 * Adds the achievement.
	 * 
	 * @param name
	 *            achievement name ex: Level One pass
	 * @param prize
	 *            achievement prize ex: you win a nice big ice cream
	 * @param description
	 *            achievement description
	 * @return int > -1 if success
	 */
	public static int addAchievement(String name, String prize,
			String description) {

		Achievement achievement = new Achievement();
		achievement.setName(name);
		achievement.setPrize(prize);
		achievement.setDescription(description);
		
		return addAchievement(achievement);
	}

	/**
	 * Add Achievement to database.
	 * 
	 * @param achievement
	 * @return id of achievement or -1 for error
	 */
	public static int addAchievement(Achievement achievement) {
		
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			tx = session.beginTransaction();
			
			result = (Integer) session.save(achievement);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return result;
	}
	
	/**
	 * Update Achievement to database
	 * 
	 * @param achievement
	 * @return true is saved, false on error.
	 */
	public static boolean updateAchievement(Achievement achievement) {
		boolean result = true;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			tx = session.beginTransaction();

			session.merge(achievement);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			result = false;
		} finally {
			session.close();
		}
		
		return result;
	}

	/**
	 * Get Achievement by achievement id
	 * 
	 * @param id id column
	 * 
	 * @return Achievement object
	 */
	public static Achievement getAchievementById(int id) {
		Achievement achievement = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Achievement.class);
			criteria.add(Restrictions.eq("id", id)).setMaxResults(1);

			achievement = (Achievement) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return achievement;
	}

	/**
	 * Get Achievement by achievement id
	 * 
	 * @param name achievement name
	 * 
	 * @return Achievement object
	 */
	public static Achievement getAchievementByName(String name) {
		Achievement achievement = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Achievement.class);
			criteria.add(Restrictions.eq("name", name)).setMaxResults(1);

			achievement = (Achievement) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return achievement;
	}

	/*
	 * Get list of Achievements
	 */
	/**
	 * Gets the achievements.
	 * 
	 * @return the achievements
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<Achievement> getAchievements() {
		ArrayList<Achievement> achievements = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			tx = session.beginTransaction();

			Query query = session
					.createQuery("From Achievement order by name asc");

			achievements = (ArrayList<Achievement>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return achievements;
	}
	
	/**
	 * Delete achievement
	 * 
	 * @param achievement
	 */
	public static void deleteAchievement(Achievement achievement) {
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			tx = session.beginTransaction();
			if (achievement != null) {
				session.delete(achievement);
			}

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}